package utils

import (
	"github.com/golang-jwt/jwt/v4"
	"reflect"
	"strings"
	"time"
)

// 获取当前时间戳
func GetCurTime() int64 {
	currentTime := time.Now().UnixNano()
	// 将纳秒转换为秒
	currentTimeInSeconds := currentTime / int64(time.Second)
	return currentTimeInSeconds
}
func GetCurDateTime() string {
	// 获取当前时间
	currentTime := time.Now()

	// 格式化输出为指定格式
	formattedTime := currentTime.Format("2006-01-02 15:04:05")

	return formattedTime
}

// @secretKey: JWT 加解密密钥
// @seconds: 过期时间，单位秒
// @payload: 数据载体
func GetJwtToken(secretKey string, seconds int64, payload string) (string, error) {
	claims := make(jwt.MapClaims)
	currentTime := time.Now()
	claims["exp"] = currentTime.Unix() + seconds
	claims["iat"] = currentTime.Unix()
	claims["payload"] = payload
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}
func PreProcessParams(data interface{}) {
	val := reflect.ValueOf(data).Elem()
	for i := 0; i < val.NumField(); i++ {
		field := val.Field(i)
		if field.Kind() == reflect.String {
			field.SetString(strings.TrimSpace(field.String()))
		}
	}
}
